﻿@using System.Web.Mvc.Html;
@{
    PagingInfo paging = ViewBag.Paging as PagingInfo;
    int rowsPerPage = paging == null ? 10 : paging.PageSize;
    var grid = new WebGrid(rowsPerPage: rowsPerPage);
    object obj = null;
    Type modelType = ViewData.Model.GetType() as Type;
    if (modelType.IsGenericType)
    {
        modelType = modelType.GetGenericArguments()[0];
        obj = Activator.CreateInstance(modelType);
    }
    ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(() => obj, modelType);
    var props = metadata.Properties.Where(o => o.ShowForGrid() && o.IsComplexType == false);
    var operations = metadata.Operations().Where(o => o.ShowOnView == "List").OrderBy(o => o.Order);
    ViewDataDictionary viewData = new ViewDataDictionary(this.ViewData);
    viewData.Add("Operations", operations);
}
@if (this.Model == null)
{
    <div>@Locale.GetString("Vit.Web", "EmptyData")</div>
}
else
{
    <table id="list-table" class="list-table">
        <thead>
            <tr>
                <th>
                    <input type="checkbox" onclick="if(this.checked) { $('#list-form input:checkbox').attr('checked', 'checked'); } else { $('#list-form input:checkbox').removeAttr('checked'); }" />
                </th>
                @foreach (var p in props)
                {
                    <th>
                        @p.DisplayName
                    </th>
                }
                <th>
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var o in this.Model)
            {
                HtmlHelper htmlHelper = Vit.Web.Extensions.HtmlHelperExtensions.SetModel((HtmlHelper)Html, o);
                string key = string.Join("$", ViewBag.DataProvider.GetKeys(o));
                <tr>
                    <td>
                        <input type="checkbox" id="cb_@(key)" name="cb_@(key)" value="@key" />
                    </td>
                    @foreach (var p in props)
                    {
                        <td>
                            @htmlHelper.Display(p.PropertyName, new { data = o })
                        </td>
                    }
                    <td class="operations">
                        @Html.Partial("_Operations", (object)o, viewData)
                    </td>
                </tr>
            }
        </tbody>
    </table>
            if (paging != null)
            {
    @Html.Partial("_Paging", paging)
            }
}
